package app2;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;
/**
 * 持久层
 * @author Alex
 *
 */
public class BookDao2 {
	/**
	 * 无条件分页查询书籍
	 */
	public List<Book> findAllByFY(int start,int size) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 Map<String, Object>map=new LinkedHashMap<String, Object>();
			 map.put("pstart", start);
			 map.put("psize", size);
			 //读取BookMapper.xml映射文件的SQL语句
			 return sqlSession.selectList(Book.class.getName()+".findAllByFY",map);		 
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	/**
	 * 有条件分页查询书籍
	 */
	public List<Book> findAllSalByFY(double sal, int start,int size) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 Map<String, Object>map=new LinkedHashMap<String, Object>();
			 map.put("psal", sal);
			 map.put("pstart", start);
			 map.put("psize", size);
			 //读取BookMapper.xml映射文件的SQL语句
			 return sqlSession.selectList(Book.class.getName()+".findAllSalByFY",map);		 
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	
	/**
	 * 测试
	 */
	public static void main(String[] args) throws Exception{
		//创建Test对象
		//BookDao2 dao=new BookDao2();
		
		//无条件分页查询
		/*System.out.println("---------------第一页");
		List<Book> bookList1=dao.findAllByFY(0,2);
		for (Book book : bookList1) {
			System.out.println(book.getId()+":"+book.getName()+":"+book.getSal());
		}
		System.out.println("---------------第二页");
		List<Book> bookList2=dao.findAllByFY(2,2);
		for (Book book : bookList2) {
			System.out.println(book.getId()+":"+book.getName()+":"+book.getSal());
		}
		System.out.println("---------------第三页");
		List<Book> bookList3=dao.findAllByFY(4,2);
		for (Book book : bookList3) {
			System.out.println(book.getId()+":"+book.getName()+":"+book.getSal());
		}*/
		
		//有条件分页查询
		/*System.out.println("---------------第一页");
		List<Book> bookList1=dao.findAllSalByFY(20,0,2);
		for (Book book : bookList1) {
			System.out.println(book.getId()+":"+book.getName()+":"+book.getSal());
		}
		System.out.println("---------------第二页");
		List<Book> bookList2=dao.findAllSalByFY(20,2,2);
		for (Book book : bookList2) {
			System.out.println(book.getId()+":"+book.getName()+":"+book.getSal());
		}*/

	}
}
